﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Tools.getLoginUserName(Session) == null)
        {
            Label1.Text = "您未登录";
            Button1.Visible = false;
            Button2.Visible = false;
            TextBox1.Visible = false;
        }
        else
        {
            TextBox1.Visible=true;
            Button2.Visible = true ;
            Button1.Visible=true;
            Label1.Text = "欢迎您~" + Tools.getLoginUserName(Session);
            Label2.Text = "俺的余额" + Tools.getLoginUser(Session).balance;
            if(Tools.getLoginUser(Session).balance <= 0)
            {
                Label2.Text += "请充值 预约失效！！";
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session[Constants.LOGIN_USER] = null;
        Response.Redirect("~/LoginPage.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        _user user = Constants.dataBase._user.Where(x => x.name.Equals(Session[Constants.LOGIN_USER])).First();
        try
        {
            if(Decimal.Parse(TextBox1.Text) <= 0) {
                Tools.ResopnseMessage(Response, "请输入大于0的数字");
                return;
            }
            user.balance += Decimal.Parse(TextBox1.Text);
        }catch(Exception ex)
        {
            Tools.ResopnseMessage(Response, "请输入数字");
            return;
        }

        if(user.balance > 0)
        {
            user.id_delete = true;
        }
        Constants.dataBase.SubmitChanges();
        Label1.Text = "欢迎您~" + Tools.getLoginUserName(Session);
        Label2.Text = "俺的余额" + Tools.getLoginUser(Session).balance;
        if (Tools.getLoginUser(Session).balance <= 0)
        {
            Label2.Text += "请充值 预约失效！！";
        }
        Tools.ResopnseMessage(Response, "成功");
    }
}
